﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XDL.Framework.Weapons;
using JumpNFight.Simulation;
using XDL.Framework;
using Microsoft.Xna.Framework;
using XDL.Framework.Particles;

namespace JumpNFight.Weapons
{
    class TheMaskWeaponSystem:
        IWeapon,
        IWeaponSystem
    {
        public const int BouhEvent = 20001;

        World m_world;
        AudioManager m_audio;
        ParticleSystem m_bouhPS;

        WeaponSettings m_settings;
        public WeaponSettings Settings
        {
            get
            {
                return m_settings;
            }
        }

        public TheMaskWeaponSystem(Game game)
        {
        }

        public void Initialize()
        {
            m_settings = GameServices.LoadContent<WeaponSettings>("Weapons/TheMask");
            m_audio = GameServices.GetService<AudioManager>();
            m_world = GameServices.GetService<World>();
            m_bouhPS = GameServices.GetService<ParticleSystem>("Particles/Bouh");
        }

        public void Update(GameTime gameTime)
        {
            foreach (Player it in m_world.Players)
            {
                // TODO: find out a better way to know who has the "mask" weapon
                if (it.IsAlive && it.Weapon != null && it.Weapon.Settings.Name == this.Settings.Name)
                {
                    foreach (BadGuy npc in m_world.NPCs)
                    {
                        // same floor && opposite direction?
                        if (npc.IsAlive &&
                            npc.Moveable.TileY == it.Moveable.TileY && 
                            npc.IsRunningRight != it.IsRunningRight)
                        {
                            // filter out 
                            int distance = it.IsRunningRight?npc.Moveable.TileX - it.Moveable.TileX:it.Moveable.TileX - npc.Moveable.TileX;
                            if (distance >= 0 && distance <= 2)
                            {
                                m_bouhPS.AddParticle(new Vector3(it.Position.X, it.Position.Y,0), new Vector3(0,-1,0));
                                GameServices.EventDispatcher.PostEvent(npc.ID, TheMaskWeaponSystem.BouhEvent, null);
                                // per-weapon sound
                                m_audio.PlayCue(Settings.Name + "/Fire");
                            }
                        }
                    }
                }
            }
        }

        public void Fire(int ownerID, Vector3 position, Vector3 direction)
        {
            // actually, nothing to do, this weapon "automatically" fires
        }

        public void Clear()
        {
        }
    }
}
